package handlers

import (
	"1-12zk/api/request"
	"1-12zk/api/response"
	"1-12zk/dao"
	"1-12zk/models"
	"github.com/gin-gonic/gin"
)

func AddArticle(c *gin.Context) {
	var data request.AddArticle
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	article := models.Article{
		Title:   data.Title,
		Context: data.Context,
		Author:  data.Author,
		Type:    data.Type,
	}
	err = dao.AddArticle(article)
	if err != nil {
		response.ResponseError(c, "添加失败")
		return
	}
	response.ResponseSuccess(c, "添加成功")
}

func List(c *gin.Context) {
	list, err := dao.List()
	if err != nil {
		response.ResponseError(c, "查询失败")
		return
	}
	var Lists []*models.Article
	for _, i2 := range list {
		Lists = append(Lists, &models.Article{
			Title:   i2.Title,
			Context: i2.Context,
			Author:  i2.Author,
			Type:    i2.Type,
			LikeNum: i2.LikeNum,
		})
	}
	response.ResponseSuccess(c, Lists)
}
